package com.novoda.runbuddy.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.util.DBUtil;
import com.novoda.runbuddy.util.SqlScanner;

public class ContentProviderCheckPointTest extends AndroidTestCase {
	private static final String	RUN_BUDDY_DB			= "runBuddyDB1.db";
	private static final String	DATABASE_PATH		= "/data/data/com.novoda.runbuddy/databases/" + RUN_BUDDY_DB;
	private static final int	DATABASE_VERSION	= 1;

	private static final long	ROUTE_LATITUDE		= 55851100;
	private static final long	CENTER_LONGITUDE	= -4233360;

	private static final long	CENTER_LATITUDE		= 55849004;

	private static final long	ALTITUDE			= 1002;

	private final String		ROUTE_NAME			= "Green PArk";

	// Declaring all the URIs against which we are going to do some tests.
	private static final Uri	ALL_CHECKPOINTS		= Uri.parse("content://com.novoda.runbuddy/checkpoints");
	private static final Uri	FIRST_CHECKPOINT	= Uri.parse("content://com.novoda.runbuddy/checkpoints/1");
	private SQLiteDatabase		mdb;
	
	private DBUtil dbUtil;

	@Override
	protected void setUp() throws Exception {
		dbUtil = new DBUtil(this.getContext(), RUN_BUDDY_DB);
		dbUtil.executeDB(R.raw.db_drop);
		dbUtil.createDB();
		dbUtil.insertValuesIntoDB();
		
		if (mdb == null)
			mdb = getContext().openOrCreateDatabase(RUN_BUDDY_DB, Context.MODE_PRIVATE, null);
	}


	public void testGettingAllCheckPoints() throws Exception {
		Cursor cur = getContext().getContentResolver().query(ALL_CHECKPOINTS, new String[] { "name" }, null, null, null);
		cur.moveToLast();
		String actual = cur.getString(1);
		assertEquals("ch4", actual);
	}

	public void testInsertingNewCheckPoint() throws Exception {
		ContentValues values = new ContentValues();
		values.put(RunBuddyCP.CheckPoints.ALTITUDE, ALTITUDE);
		values.put(RunBuddyCP.CheckPoints.LATITUDE, CENTER_LATITUDE);
		values.put(RunBuddyCP.CheckPoints.LONGITUDE, CENTER_LONGITUDE);

		Uri uri = getContext().getContentResolver().insert(ALL_CHECKPOINTS, values);
		assertEquals(Uri.parse("content://com.novoda.runbuddy/checkpoints/5"), uri);
	}
}
